Lesson:
Introduction to programming and C++
List of Concepts:
🔘 What is Programming?
🔘 Why do we need Programming?
🔘 Types of Programming Languages
🔘 Introduction to C++
🔘 Difference between C and C++
What is Programming?
Programming is simply creating a set of instructions for a computer to perform a specific task.Programming can be done using a variety of computer programming languages eg. C,C++,Java and the list is endless !. The best part is , out of these very many options, you can go ahead with the language of your choice to implement the concepts in the form of a logical code.
Category of Programming Languages
Programming languages that have been developed so far can majorly be categorized into: machine language, assembly language, and high-level language.
Machine Level Language:
As the name suggests, it is a low-level language made up of binary numbers or bits that a machine/computer can understand. it is a sequence of 0s and 1s. It is also known as machine code or object code. Any program that we write, finally transforms into the sequence of 0s and 1s for the computer to understand.
Assembly language
Assembly language is intended to communicate directly with a computer's hardware. Unlike the usual machine language, which consists of binary characters, assembly language is expressed in a more human readable form. In future many of you are going to learn and program microprocessors in your higher studies of jobs; that would require an expertise in assembly language.
High-level language:
It refers to the programming languages that allow the use of symbolic operators ( to signify operations, eg. +,-,*,/ etc. and symbolic names (to represent data and data structure ,eg. variable names). They are also structured with syntax and semantics to describe the computing algorithm/program. This often requires the use of a compiler or an interpreter which helps in the translation from high level human understandable code into low level machine code because the computer only works on the binary data. We will be learning and focusing on this category of language in our course.
Why do we need Programming?
Computers are ridiculously fast but contrary to the assumption of being 'smart' , they are extremely dumb machines. you might be wondering, what ? Can you imagine, they don't even know how tho check if a number is odd or even (which a 2nd grade child knows ) but if we create a program which will enable them to do this task, they can check a billion numbers for us in one second. Hence, to make a computer/machine 'smart' we need the tool of programming.
Most of the programming is done for the real world applications and not just for big rocket or other scientific mission. Internet banking, online shopping ticket booking ,in flight, all this can be conceivable due to the programs. All these things and many more are possible through the magic of programming.
Type of Programming Paradigms:
🔘 Procedural
🔘 Functional
🔘 Object Oriented
Procedural:
Consider a scenario where a program has to collect some data from users (read), perform some calculations and then display the result(print) when requested; a simple example of this scenario is any online transaction that you/your parents do In procedural approach, we can write 3 different procedures/functions for each of the operations of reading, calculating and printing(which could interact amongst each other as well ).
🔘 The entire program code is organized as a set of procedures/functions or discrete blocks of codes that are executed in an order.
🔘 These procedures are also known as subroutines or functions and contain a series of steps that will be carried out when the procedure is called/used.
🔘 Some of the programming languages that enable us to use procedural approach : BASIC,FORTRAN,ALGOL,C,COBOL,and Pascal.
Functional:
Let's assume a scenario where we have to perform lots of differents operations on the same set of dada. This can be in the domains such as the web, statistics, financial analysis, and machine learning. Here, the functional programming paradigm helps a lot.
🔘 The program code is organized in pure functions(which always hield the same value for the same set of inputs without any deviation) with each function set up to perform a clearly defined task.
🔘 The data/variables are passed in the function as parameters (for the functions to interact wit other functions or programs).
🔘 Languages that support this approach are: Javascript, Python, etc.
Object Oriented:
🔘 The program data is organized in classes and objects rather than functions and logic.
🔘 A class is a blueprint for crating objects and an object is referred to as an instance of a class that has unique attributes and behavior.
🔘 A good example of this could be you, yes you! can be a student or a working professional. in this scenario, under the class of ' person', we can have 'student' or 'Working professional' class and you can be an instance of any of these based on whether you fall in the student category or working professional category. Hence, you are an object
🔘 Languages that support object-oriented approach include: Java, C++, C#, Python, PHP, Visual Basic.
The programming language that we would learn in the very first go has to be a simple yet most capable one! yes, you guessed it right, it's C++ !!
Lets now, hit the bulls eye
Let's get started.
Introduction to C++
🔘 C++ is a popular programming language.
🔘 C++ is a middle-level programming language.
🔘 C++ is developed by Bjarne Stroustrup in 1979 at Bell Labs.
🔘 C++ runs on a variety of platforms, such as Windows, mac os and the various versions of UNIX.
🔘 C++ is a superset of the C language.
🔘 C++ is closer to the compiler and faster than C language.
🔘 C++ is a statically typed programming language i.e it uses static typing when type checking is performed during compile-time as opposed to run-time.
🔘 C++ is both a procedural and an object-oriented programming language. It supports OOP features such as polymorphism, encapsulation, and inheritance.
🔘 As C++ is an object -oriented programming language so it gives a clear structure to programs and allows code to be reused, lowering development costs.
Some basic differences between C and C++ before we move ahead:
🔘 C is structural programming language that doesn't provide any support for classes and objects but C++ is an object-oriented programming language, and it provides support for the concepts for classes and objects.
🔘 C primarily supports procedural programming for developing codes whereas C++ supports both programming paradigms-procedural as well as object-oriented.
🔘 The C language uses the scanf() function for the input operation and printf() function for the output operations whereas C++ language uses cin function for the input operation and cout function for the output.
🔘 C++ is closer to the compiler and faster than C language.
Visual Studio Code Installation on Windows Step by Step
1. Click this link to download MinGW and click on download in Green as shown.
2. After downloading, click on install and Next.
3. Open the MinGW installation manager and click and Mark All of them for installation as shown below.
4. Click on installation on Top right, then apply changes as shown.
5. Now we will add the MinGW to Path in Environment Variables. To do this, go to
This PC→Right click→Propertis→Advanced System Settings
6. Click on Environment Variables.
7. Go to "Path" and click on "Edit" as shown below.
8. Now click on New and add the address of MinGW Bin as shown below.
The path will be → C:\MinGW\bin and C:\MinGW\bin\g++.exe
9. Now just Click on "OK"
Downloading VSCode
2. Now install VSCode. Keep on clicking next and make sure to checkbox "add to path" as shown.
4. Click on Extensions on the left column as highlighted (in yellow).
5. Now install the C/C++ extensions by searching it on the search Bar and click on install button.
6. Also, install the "Code Runner" extension by searching it on the search bar and click on install.
7. To run the code click on "Run Code" on the top right as shown below.
C++ Variables
🔘 A variable is the title of a reserved region allocated in memory. In other words, it may be referred to as the name of a memory location.
🔘 A variable is a container that holds the value while the c++ program is executed.
🔘 Each variable should be given a unique name to indicate the storage area.
🔘 A variables assigned with a data type ( we will learn about it after this.)
Syntax for Declaring a Variable:
Type variable_name[=value];
The variable_name is the name of a variable. we can initialize the variable by specifying an equal sign and a value (initialization i.e. assigning an initial value, is optional).
int age=20;
Name Conventions for variables in C++
Like us, all c++ components are identified with their names. There are a few points to remember while naming the variable. They are as follows-
🔘 Variable names should not begin with a number. for example
int 2var; //2var is an invalid variable.
🔘 Whitespaces are not permitted in variable names. for example
int cricket score; // invalid variables.
🔘 A C++ keyword(reserved word) cannot be used as a variable name. for examples, int float; is an invalid expression as float is pre-defined as a keyword(we will learn about them) in C++.
🔘 As per the latest coding practices, for variable names with more than one word the first word has all lowercase letters and first letter of subsequent words capitalised. for example, cricketScore, codePracticeProgram etc.
🔘 While creating variables it's preferable to give them meaningful names like- 'age','earning','value' etc. for instance, makes much more sense than variable names like a, e, and v.
🔘 We use all lowercase letters when creating one-word variable name. It's preferable(and in practice) to use physics rather than PHYSICS or pHYSICS.
C++ Identifiers
C++ identifier is a name given to a package, class, interface, method, or variable. All identifiers must have different names.
In C++ there are a few points to remember while dealing with identifiers:
🔘 Rule 1 All identifiers should begin with a letter (A to Z or a to z), $ and must be unique.
🔘 Rule 2 After the first character/letter, identifiers can have any combination of characters.
🔘 Rule 3 A keyword cannot be used as an identifier.
🔘 Rule 4 The identifiers are case sensitive.
🔘 Rule 5 Whitespaces are not premitted.
Examples of legal identifiers: rank, $name, _rate,__2_mark.
Examples of illegal identifiers: 102pqr, -name.
Basic Memory units:
It refers to the amount of memory or storage used to measure data.
Basic memory units are:
1. Bit
A bit (binary digit 0 or 1) is the smallest unit of data that acomputer can process and store symbols 0 and 1 are known as bits. Here, 0 indicates the passive state of signal and 1 indicates the active state of signal.
At a time, a bit can store only one value i.e 0 or 1. To have a greater range of value, we combine multiple bits.
2. Byte
A byte is unit of memory/data that is equal to 8 bits.
you may think of a byte as one letter. For example, the letter 'f' is one byte or eight bits.
The bigger units are:
3. Kilobyte
A kilobyte is a unit memory data equal to 1024 bytes.
4. Megabyte
A megabyte is a unit of memory data equal to 1024 kilobytes.
5. Gigabyte
A Gigabyte is a unit of memory data equal to 024 Megabytes.
C++ Data Types
Data types specify the different sizes and values that can be stored in the variable. Based on the data type of a variable, the operating system allocates memory and decides what can be stored in the reserved memory. Therefore, by assigning different data types to variables , we can store integers, decimals, or character in these variables.
Data types in C++ are categorized in three groups: Built-in (primary), user-defined and Derived.
1. C++ int
🔘 The int keyword is used to indicate integers.
🔘 Its size is usually 4 bytes. Meaning, values can be stored from - 2147483648 to 2147483647.
🔘 For example: int rate=40;
2. C++ Char
🔘 This data type is used to store a single character.
🔘 Size of char is 1 byte.
🔘 Characters in C++ are enclosed inside single quotes ' '
🔘 For example: char symbol='a'
3. C++ bool
🔘 A boolean data type is declared with the bool keyword.
🔘 The boo data type has one of two possible values: true or false.
🔘 For example bool flag=false:
4. C++ float
🔘 float is used to store floating point numbers (decimals and exponential).
🔘 The size of the float is 4 bytes.
🔘 In general, 7 decimal digits precision.
🔘 For example: float rate=40.50;
5. C++ double
🔘 Double used to store floating-point numbers (decimal and exponentials).
🔘 the size of the double is 8 bytes. Hence, double has two times the precision of float.
🔘 In general, 15 decimal digits precision.
🔘 for example: double rate=40.654344;
6. C++ void
🔘 The void keyword means "nothing" or "no value".
🔘 Void will be used in functions and pointers.
🔘 Variables of the void type can't be declared.
7. C++ wchar_t
🔘 Wide character wchar_t is similar to the char data type, except its size is 2 bytes instead of 1.
Note: C++ String Data Types
🔘 This data type is used to store a sequence of characters (text).
🔘 This is not a built-in data type.
🔘 String values must be surrounded by double quotes:
🔘 For example string name="Ramesh";
C++ Output/Display Program
//C++ program to display "Hello World in C++":
#include<iostream>
using namespace std;
int main()
{
//prints hello world in C++
cout<<"Hello world in C++";
return 0;
}
Output: Hello World in C++
What is
C++? Why its support concept of oops.
C++ is an object-oriented programming language and it is
also known as structure programming language. C++ is developed by ‘Bjarne
Stroustrop’ at AT &T’S Bell laboratory (USA) in 1979.
Why It is
support oops concept: -
Because C++ is a structure of object-oriented programming so,
it supports concept of oops.
Feature of
C++
C++ provides So, many features such as Array, structure,
class and object, constructor and destructor, inheritance, polymorphism,
containership, encapsulation, sorting, queue and linked list etc….
Note: -
1.
C++ is a case sensitive.
2.
Any types of program written in same letter.
3.
.cpp is an extension of c++ file.
4.
Execution of program to ctrl+f9.
Syntax or
Structure of C++
#include<iostream.h> ß
Header file // # means
pre-processor
#include<conio.h>
void main()
{
clrscr();
statement;
getch();
}
What is main() ? Explain with example.
1.
Main is a user defined function because user is
responsible to define the logic inside the main function.
2.
The execution of program always starts and ends
with main function.
3.
Each and every program has atleast one function
which is main function.
4.
It is crucial part of any program.
5.
The main function may contain any number of
statements.
Syntax: -
return
type main()
{
//
code;
}
Type of
function: -
1.
User define
2.
Pre-define
Program
Debugging: -
·
After writing a program the next step is to
debug the program.
|
Error |
|
Syntax |
|
Runtime |
|
Logical error |
Syntax error: - Syntax
error occurs when we violate any grammatical rules of programming language.
Ex: -
main()
{
int
a;
cout<<”Enter
a number”;
cin>>a à cin>>a // is syntax error because ;(semicolon) not
present cin>>a; here
}
Runtime error: - Runtime
error is any error that causes abnormal program termination during the
execution.
Ex: -
main()
{
int
a;
A=10/0; Ã
runtime error because 10/0 are not possible
aout<<a;
}
Logical error: - a
logical error simply an incorrect translation of either the problem statement
or the algorithm.
What is
class? Discuss types of class: -
1.
Class is a user define datatype which is
declared by keyword class.
2.
Class contain it’s own data member and member
function.
3.
These data member and member function accessed
by three access specifiers (private, protected, public).
4.
By default, the access specifier of class is private.
Type of
class: -
·
Local
·
Global
·
Friend
·
Template
·
Abstract
·
Static
What is
Local Class? With program.
A class which is declared inside a function or block is
called local class.
Syntax: -
Return
type function name
{
Class
class_name
{
};
}
main()
{
Function_name;
}
#include<iostream.h>
#include<conio.h>
void fun();
{
class
ab
{
private:
int
a,b;
public:
void
show()
{
cout<<"Enter
two number: ";
cin>>a>>b;
cout<<a<<"
"<<b;
}
};
ab obj;
obj.show();
}
void main()
{
clrscr();
fun();
getch();
}
What is
global class? Discus with program.
A class which declared outside of all the functions or
block.
Example: -
#include<iostream.h>
#include<conio.h>
class hemant
{
protected:
int
a,b;
public:
void
input()
{
cout<<"Enter
two numbers: ";
cin>>a>>b;
}
void
show()
{
cout<<a<<"
"<<b<<endl;
}
};
class hemant1:public hemant
{
public:
void
disp()
{
cout<<a<<"
"<<b;
}
};
void main()
{
clrscr();
// hemant obj;
hemant1
obj2;
obj2.input();
obj2.show();
obj2.disp();
getch();
}
What is
nested class? Explain with example.
A class which is declared or contain inside another class
called nested class.
Example: -
#include"iostream.h"
#include"conio.h"
class hemant
{
public:
class
hemant1 //member variable of class
hemant
{
int
a,b;
public:
void
input() //member function of class
hemant1
{
cout<<"enter
two number: ";
cin>>a>>b;
}
void
show()
{
cout<<a<<"
"<<b<<endl;
}
};
};
void main()
{
clrscr();
hemant::hemant1
obj;
obj.input();
obj.show();
getch();
}
Access Specifier: -
1.
Private
·
Access by member of class
·
Access by friend
2.
Protected
3.
Public
Private
access specifier: -
#include"iostream.h"
#include"conio.h"
class hemant
{
private:
int
a,b;
public:
void
show()
{
a=30,b=40;
cout<<a<<"
"<<b<<endl;
}
friend
class hemant1;
};
class hemant1
{
public:
void
disp(hemant ref)
{
ref.a=20,ref.b=10;
cout<<ref.a<<"
"<<ref.b<<endl;
}
};
void main()
{
clrscr();
hemant
obj; hemant1 obj1;
obj.show();
obj1.disp(obj);
getch();
}
Protected
specifier: -
#include"iostream.h"
#include"conio.h"
class base
{
protected:
int
a,b;
public:
void
show()
{
a=20,b=30;
cout<<a<<endl<<b<<endl;
}
};
class Derive:public base
{
public:
void
disp()
{
a=40,b=50;
cout<<a<<endl<<b<<endl;
}
};
void main()
{
clrscr();
Derive
obj;
obj.show();
obj.disp();
getch();
}
Public
access specifier: -
#include"iostream.h"
#include"conio.h"
class hemant
{
public:
int a;
void
show();
};
void hemant::show()
{
a=20;
cout<<"out
side "<<a<<endl;
}
void main()
{
clrscr();
hemant
obj;
obj.show();
obj.a=20;
cout<<"outside
"<<obj.a;
getch();
}
What is
class and object? And how many access specifiers in class.
Class: -
è
Class is a user defined datatype, which holds
its own data member and member function in other word we can say class is
collection of data member and member function. Which can access and use by
creating object of that class.
Object: -
è
An object is an instance of a class. Whenever
class is defined, no memory is allocated by when object is initialized memory
is allocated of that class.
There are
three types of access specifier in class: -
1.
Private
2.
Public
3.
Protected
Declaration
of class: -
Syntax: -
Class
class_name
{
Private:
Data
member
Member
function
Public:
Data
member
Member
function
Protected:
Data
member
Member
function
};
Every class ends with semi-colon
Example: -
#include"iostream.h"
#include"conio.h"
class demo
{
private:
int
a,b;
public:
void
input()
{
cout<<"Enter
two number ";
cin>>a>>b;
}
void
show()
{
cout<<a<<endl<<b;
}
};
void main()
{
clrscr();
demo
obj;
obj.input();
obj.show();
getch();
}
Declaration
of object: -
Syntax: -
Class_name
object_name1, object_name2……….;
Ex: -
Demo
obj1, obj2……….;
#include"iostream.h"
#include"conio.h"
class demo
{
private:
int
a,b;
public:
void
input()
{
cout<<"Enter
two number ";
cin>>a>>b;
}
void
show()
{
cout<<a<<endl<<b;
}
};
void main()
{
clrscr();
demo
obj,obj1;
obj.input();
obj.show();
obj1.input();
obj1.show();
getch();
}
Print hello
india in c++
#include<iostream.h>
#include<conio.h>
class hemant
{
public:
void
show()
{
cout<<"Hell
india.........";
}
};
void main()
{
clrscr();
hemant
obj;
obj.show();
getch();
}
Sum of two
number: -
#include<iostream.h>
#include<conio.h>
class add
{
private:
int
a,b,sum;
public:
void
show()
{
cout<<"Enter
two number: ";
cin>>a>>b;
}
void
out()
{
sum=a+b;
}
void
output()
{
cout<<"sum
is "<<sum;
}
};
void main()
{
clrscr();
add
obj;
obj.show();
obj.out();
obj.output();
getch();
}
Sum of two
number: -
#include<iostream.h>
#include<conio.h>
class add
{
private:
int
a,b,sum;
public:
void
show()
{
cout<<"Enter
two number: ";
cin>>a>>b;
sum=a+b;
cout<<"sum
is "<<sum;
}
};
void main()
{
clrscr();
add
obj;
obj.show();
getch();
}
Mathematical
operator: -
#include<iostream.h>
#include<conio.h>
class math_operator
{
private:
int
num1,num2,sum,mult,div,sub;
public:
void
input()
{
cout<<"Enter
two number: ";
cin>>num1>>num2;
}
void
process()
{
sum=num1+num2;
sub=num1-num2;
mult=num1*num2;
div=num1/num2;
}
void
output()
{
cout<<"sum
is "<<sum<<endl;
cout<<"subtract
is "<<sub<<endl;
cout<<"mult
is "<<mult<<endl;
cout<<"divide
is "<<div<<endl;
}
};
void main()
{
clrscr();
math_operator
obj;
obj.input();
obj.process();
obj.output();
getch();
}
Inheritance:
-
Inheritance is a mechanism in which one class inherits the
property of other class is known as inheritance.
In
other word we can say when one class access the property of another class is
called inheritance.
There are four types
of inheritance: -
1. Single inheritance/simple inheritance
2. Multi-level inheritance
3. Multiple inheritance
4. Hierarchical inheritance
5. Hybrid inheritance
1. Simple inheritance
|
Base |
|
Derive |
2. Multi-level inheritance
|
Base |
|
Derive1 |
|
Derive2 |
3. Multiple inheritance
|
Base1 |
|
Base2 |
|
Derive |
4. Hierarchical inheritance
|
Base |
|
Derive1 |
|
Derive2 |
5. Hybrid inheritance
|
Multi-level
inheritance |
|
Base |
|
Derive1 |
|
Derive2 |
|
Derive3 |
|
Multi-level
inheritance |
1.
Single
inheritance: -
è
A class which contain only one base class and
only derive class is called single inheritance.
Syntax: -
Class base
{
Member
of class
};
Class derive: public/private/protected
{
Member
of derive class
};
Example: -
#include<iostream.h>
#include<conio.h>
class base
{
private:
int
a,b;
public:
void
input()
{
cout<<"Enter
two values: ";
cin>>a>>b;
}
void
show()
{
cout<<a<<"
"<<b<<endl;
}
};
class derive:public base
{
private:
int
m,n;
public:
void
disp()
{
cout<<"Enter
two values: ";
cin>>m>>n;
cout<<m<<"
"<<n;
}
};
void main()
{
clrscr();
base
obj;
derive
obj1;
obj1.input();
obj1.show();
obj1.disp();
getch();
}
2.
Multi-level
inheritance: -
è
A class which contain only one base class and
multiple derive class is called multi-level inheritance.
Syntax: -
Class base
{
Member
of base class
}
Class derive1: public/private/protected base
{
Member
of derive1 class
}
Class derive2: public/private/protected derive1
{
Member
of derive2 class
}
Example: -
#include<iostream.h>
#include<conio.h>
class base
{
private:
int a;
public:
void
input()
{
cout<<"Enter
two values: ";
cin>>a;
}
void
show()
{
cout<<"a="<<a<<endl;
}
};
class derive:public base
{
private:
int b;
public:
void
input1()
{
cout<<"Enter
two values: ";
cin>>b;
}
void
show1()
{
cout<<"b="<<b<<endl;
}
};
class derive1:public derive
{
private:
int c;
public:
void
input2()
{
cout<<"Enter
two values: ";
cin>>c;
}
void
show2()
{
cout<<"c="<<c<<endl;
}
};
void main()
{
clrscr();
base
obj;
obj.input();
obj.show();
derive
obj1;
obj1.input1();
obj1.show1();
derive1
obj2;
obj2.input2();
obj2.show2();
getch();
}
Example: -
#include<iostream.h>
#include<conio.h>
class base
{
protected:
int a;
public:
void
input()
{
cout<<"Enter
two values: ";
cin>>a;
}
void
show()
{
cout<<"a="<<a<<endl;
}
};
class derive:public base
{
protected:
int b;
public:
void
input1()
{
cout<<"Enter
two values: ";
cin>>b;
}
void
show1()
{
cout<<"b="<<b<<endl;
}
};
class derive1:public derive
{
private:
int c;
public:
void
input2()
{
cout<<"Enter
two values: ";
cin>>c;
}
void
show2()
{
cout<<"a="<<a<<"
"<<"b="<<b<<"
"<<"c="<<c<<endl;
}
};
void main()
{
clrscr();
/* base obj;
obj.input();
obj.show();
derive
obj1;
obj1.input1();
obj1.show1();*/
derive1
obj2;
obj2.input();
obj2.show();
obj2.input1();
obj2.show1();
obj2.input2();
obj2.show2();
getch();
}
Multiple
Inheritance: -
è
A class which contain more base class and only
one derive class is called multiple inertance.
Syntax: -
Class base1
{
Member
of class base1
};
Class 2
{
Member
of class base2
};
Class derive: public/private/protect base1, base2
{
Member
of derive class
};
Example: -
#include<iostream.h>
#include<conio.h>
class base
{
private:
int
a,b,c;
public:
void
input()
{
cout<<"Enter
two number: ";
cin>>a>>b;
}
void
show()
{
c=a+b;
cout<<"sum="<<c;
}
};
class base1
{
private:
int
a,b,c;
public:
void
input1()
{
cout<<"Enter
two number: ";
cin>>a>>b;
}
void
show1()
{
c=a-b;
cout<<"subtract="<<c;
}
};
class derive:public base,public base1
{
private:
int
a,b,c;
public:
void
input2()
{
cout<<"Enter
two number: ";
cin>>a>>b;
}
void
show2()
{
c=a*b;
cout<<"multiply="<<c;
}
};
void main()
{
clrscr();
/* base obj;
obj.input();
obj.show();
base1
obj1;
obj1.input1();
obj1.show1();*/
derive
obj2;
obj2.input2();
obj2.show2();
obj2.input();
obj2.show();
obj2.input1();
obj2.show1();
getch();
}
Hierarchical
Inheritance: -
è
A class which contain only one base and multiple
derive class is called hierarchical Inheritance.
Syntax: -
Class base
{
Member
of base class
};
Class derive1: public/private/protected base
{
Member
of derive1 class
};
Class derive2: public/private/protected base
{
Member
of derive2 class
};
Example: -
#include<iostream.h>
#include<conio.h>
class base
{
protected:
int
a,b;
public:
void
input()
{
cout<<"Enter
two number: ";
cin>>a>>b;
}
void
show()
{
cout<<"sum="<<a+b;
}
};
class derive:public base
{
private:
int n1;
public:
void
input1()
{
cout<<"Enter
a number: ";
cin>>n1;
}
void
show1()
{
cout<<n1<<endl<<a+b;
}
};
class derive1:public base
{
private:
int n2;
public:
void
input2()
{
cout<<"Enter
a number: ";
cin>>n2;
}
void
show2()
{
cout<<n2<<endl<<a+b;
}
};
void main()
{
clrscr();
derive1
obj;
obj.input();
obj.input2();
obj.show2();
getch();
}
Hybrid
Inheritance
è
It is the combination of more than one type of
inheritance is called hybrid inheritance.
Syntax: -
Class a
{
Member
of base class
};
Class b:virtual public/private/protected a
{
Member
of derive1 class
};
Class c:virtual/public/private/protected a
{
Member
of derive2 class
};
Class d:public a
{
};
Example: -
#include<iostream.h>
#include<conio.h>
class a
{
private:
int n;
public:
void
input()
{
cout<<"Enter
a number: ";
cin>>n;
}
void
show()
{
cout<<n;
}
};
class b:virtual public a
{
};
class c:virtual public b
{
};
class d: public c
{
};
void main()
{
clrscr();
a obj;
b obj1; c obj2; d obj3;
obj.input();
obj.show();
obj1.input();
obj1.show();
obj2.input();
obj2.show();
obj3.input();
obj3.show();
getch();
}
What is
function overloading? Full explanation.
è
Whenever a program contains more than one
function with same name different types of parameters called function
overloading.
Syntax: -
Class class_name
{
Public:
Void
add()
{
}
Void
add(int a)
{
}
};
Example: -
#include<iostream.h>
#include<conio.h>
class over
{
private:
int
a,b,sum;
public:
void
add()
{
cout<<"Enter
two number: ";
cin>>a>>b;
sum=a+b;
cout<<"sum
"<<sum<<endl;
}
void
add(int a,int b)
{
int
sub=a-b;
cout<<"sub
"<<sub;
}
};
void main()
{
clrscr();
over
obj;
obj.add();
int
n=200,m=100;
obj.add(n,m);
getch();
}
What is
function overriding? Full explanation.
è
Whenever we writing function in base and derived
classes in such a way that function name, parameter must be same called
function overriding.
Syntax: -
Class class_name
{
Void
fun()
{
}
};
Class class_name:public base_class
{
Void
fun()
{
}
};
Example: -
#include<iostream.h>
#include<conio.h>
class base
{
public:
void
fun()
{
cout<<"Hemant";
}
};
class derive:public base
{
public:
void
fun()
{
cout<<"Hemant
Kumar";
}
};
void main()
{
clrscr();
derive
obj;
obj.fun();
getch();
}
Example: -
#include<iostream.h>
#include<conio.h>
class base
{
public:
void
fun()
{
cout<<"Hemant";
}
};
class derive:public base
{
public:
void
fun()
{
cout<<"Hemant
Kumar";
}
};
void main()
{
clrscr();
derive
obj;
obj.base::fun();
getch();
}
What is
polymorphism? Explain with example.
è
Polymorphism is the combination of two Greek
word one is poly means many and another is morphism means form.
è
“whose meaning is same object having different
behaviour”
Types: -
1.
Compile time polymorphism.
2.
Runtime polymorphism.
Compile
time: -
Compile time polymorphism achieved by compile time called
static polymorphism.
Ex: - method overloading
Runtime
polymorphism: -
A polymorphism which is achieved by runtime called dynamic
polymorphism.
Ex: - method overriding
Explain
with example: -
Compile
time
Void add(int x, int y)
{
}
Void add(int x, float y)
{
}
Runtime: -
Void add()//base class
{
}
Void add()//derived
{
}
What is
compile time polymorphism?
A polymorphism which is achieved by compile time called or
static polymorphism
Method overloading is an example of compile time polymorphism.
Example: -
#include<iostream.h>
#include<conio.h>
class base
{
float
a,b;
public:
void
show(int x, int y)
{
a=x;b=y;
cout<<a<<"
"<<b<<endl;
}
void
show(float x, float y)
{
a=x;b=y;
cout<<a<<"
"<<b<<endl;
}
};
void main()
{
clrscr();
base
obj;
obj.show(3,4);
obj.show(3.4f,5.6f);
getch();
}
Runtime: -
Example: -
#include<iostream.h>
#include<conio.h>
class base
{
public:
void
show()
{
cout<<"Hiii
i am base class"<<endl;
}
};
class derive: public base
{
public:
void
show()
{
cout<<"Hiii
i am derive class";
}
};
void main()
{
clrscr();
derive
obj;
obj.base::show();
obj.show();
getch();
}
What is
encapsulation? Explain with example.
It is one of the most important features of oops that used
to wrapping the data and function into a single unit. The data of class is not
accessible to outside the class, only those function access data which are
wrapped in the class.
Ex: - class is an
example of encapsulation, which talk about binding the data & function
together that manipulates those data.
#include<iostream.h>
#include<conio.h>
class base
{
int
a,b;
public:
void
show()
{
cout<<"Enter
two number: ";
cin>>a>>b;
cout<<a<<"
"<<b<<endl;
}
void
sum()
{
cout<<"Enter
two number: ";
cin>>a>>b;
cout<<"sum
is "<<a+b;
}
};
void main()
{
clrscr();
base
obj,obj1;
obj.show();
obj1.sum();
getch();
}
Example: -
#include<iostream.h>
#include<conio.h>
class base
{
// int a,b;
public:
void
show()
{
cout<<"Enter
two number: ";
cin>>a>>b;
cout<<a<<"
"<<b<<endl;
}
void
sum()
{
cout<<"Enter
two number: ";
cin>>a>>b;
cout<<"sum
is "<<a+b;
}
private:
int
a,b;
};
void main()
{
clrscr();
base
obj,obj1;
obj.show();
obj1.sum();
getch();
}
What is
abstraction? Explain with example.
Abstraction Is the one of the most important features of
oops which is showing only the essential information to the outside world and
hiding the background details.
Abstraction: -
n
Class
n
Header-file
Why we use constructor
in C++?
Constructor is a special member function of class which is
used to create and initialize the objects.
feature: -
1.
A constructor can only have one access modifier
which is public.
2.
A constructor is never inherited &
overridden.
3.
Each and every C++ class has constructor, either
it is provided by compile by default explicitly created by the user.
What is Constructor and destructor? Explain with
example.
Constructor: -
1.
constructor is a special type of function which
has the same name as the class name.
2.
constructor Is being automatically call at the
time of object declaration.
3.
The return type od the constructor is the class
type.
Destructor: -
1.
Destructor is a special member function that is
executed automatically when an object is destroyed that has been created by the
constructor.
2.
Destructors are used to de-allocate the memory
that has been allocated for the object by the constructor.
3.
A destructor declaration should always begin
with the tilde (~) symbol as shown in the following example: -
#include<iostream.h>
#include<conio.h>
class test
{
Public:
test()
{
N=10;
cout<<n;
}
~test()
{
cout<<”object
destroyed”;
}
};
void main()
{
test
ob;
getch();
}
There are three
types of constructor: -
i)
Default constructor
ii)
Parametrized constructor
iii) Copy constructor
What is
default constructor? With example: -
A constructor that accepts no parameters is called default
constructor.
Syntax: -
Class_name()
{
Code
}
#include<iostream.h>
#include<conio.h>
class Hem
{
int
a; // by default private
public:
Hem()
{
a=100;
cout<<a;
}
};
void main()
{
clrscr();
Hem
obj;
getch();
}
What is
parametrized constructor? With example.
A constructor that accepts or receive parameters is called
parametrized constructor.
Syntax: -
Class_name(para1, para2…….)
{
Code
}
#include<iostream.h>
#include<conio.h>
class Hem
{
private:
int
a,b;
public:
Hem(int
x, int y)
{
a=x;
b=y;
}
void
show()
{
cout<<a<<"
"<<b;
}
};
void main()
{
clrscr();
Hem
obj(10,20);
obj.show();
getch();
}
What is copy
constructor? With example……
A constructor that is used to copy or initialize the value
of one object into another object is called copy constructor.
Syntax: -
Class_name(class_name &ref)
{
Code;
}
#include<iostream.h>
#include<conio.h>
class Hem
{
private:
int
a,b;
public:
Hem(int
x, int y)
{
a=x;
b=y;
}
Hem(Hem
&ref)
{
a=ref.a;
b=ref.b;
}
void
show()
{
cout<<a<<"
"<<b<<endl;
}
};
void main()
{
clrscr();
Hem
obj(10,20);
Hem
obj2=obj;
obj.show();
obj2.show();
getch();
}
Type of constructor example (default parameterized copy constructor): -
#include<iostream.h>
#include<conio.h>
class test
{
private:
int
a,b;
public:
test()
{
a=2;
b=4;
cout<<a<<"
"<<b<<endl;
}
test(int
x,int y)
{
a=x;
b=y;
cout<<a<<endl<<b<<endl;
}
test(test
&obj)
{
a=obj.a;
b=obj.b;
cout<<a<<ends<<b;
}
};
void main()
{
clrscr();
test
ob,ob2(10,20);
test
ob3(ob2);
getch();
}
Difference
between constructor and destructor?
|
1. It
allocates memory to an object. |
It
de-allocate the memory of an object. |
|
2. The
name of constructor is the same name as the class name. |
The
name of destructor is the same name as the class name but preceding tilde
sign (~). |
|
3. It
is being automatically called at the time of object declaration. |
It
is automatically called at the time of object termination. |
|
4. We
can pass arguments through constructor. |
We
can’t pass any arguments through destructor. |
|
5. We
can declare multiple constructor in a class |
Only
one destructor allows in a class. |
|
6. Constructor
can be overloaded. |
But,
overloading is not possible. |
|
7.
Constructor have various types like default,
parametrized etc. Syntax: -
class_name(args) { Code } |
No any types of destructor. Syntax: -
~class_name() { Code } |
working in process....................

.png)
.png)

0 Comments